import request from '../index';

// 模拟数据，用于开发测试
const mockData = {
  // 模拟头条分类数据
  categories: [
    { id: 1, name: '推荐', isSelected: true },
    { id: 2, name: '活动促销' },
    { id: 3, name: '新店开业' },
    { id: 4, name: '服务升级' },
    { id: 5, name: '优惠信息' },
    { id: 6, name: '行业动态' }
  ],
  
  // 模拟头条列表数据
  list: [
    {
      id: '1',
      title: '美食探店：这家新开的川菜馆人均不到50元，菜品却让人惊艳！',
      desc: '最近，在商业中心附近新开了一家川菜馆，名叫"川味小馆"。虽然人均消费不到50元，但是菜品质量却让人惊艳...',
      cover: '',
      addTime: '2023-06-15 10:30:00',
      viewNum: 1283,
      likeNum: 89,
      commentNum: 23,
      type: 1
    },
    {
      id: '2',
      title: '重磅！市中心大型购物中心将于下月开业，超200家品牌入驻',
      desc: '期待已久的市中心大型购物中心终于要开业了！据了解，该购物中心总建筑面积达15万平方米，将有超过200家品牌入驻...',
      cover: '',
      addTime: '2023-06-14 14:20:00',
      viewNum: 2456,
      likeNum: 156,
      commentNum: 42,
      type: 3
    },
    {
      id: '3',
      title: '夏日清凉特惠：全场饮品第二杯半价，限时一周！',
      desc: '炎炎夏日，来一杯清凉饮品解暑吧！即日起至本周日，市内多家饮品店联合推出"第二杯半价"活动...',
      cover: '',
      addTime: '2023-06-13 09:15:00',
      viewNum: 1876,
      likeNum: 103,
      commentNum: 28,
      type: 2
    },
    {
      id: '4',
      title: '本地生活服务平台升级：新增家政保洁、维修服务等多个品类',
      desc: '为了更好地服务广大用户，我们的本地生活服务平台进行了全面升级，新增了家政保洁、上门维修、教育培训等多个服务品类...',
      cover: '',
      addTime: '2023-06-12 16:40:00',
      viewNum: 1123,
      likeNum: 75,
      commentNum: 19,
      type: 4
    },
    {
      id: '5',
      title: '会员专享：新用户注册即送20元无门槛优惠券',
      desc: '好消息！即日起，新用户注册我们的平台即可获得20元无门槛优惠券，可在平台内任意商家使用...',
      cover: '',
      addTime: '2023-06-11 11:30:00',
      viewNum: 2134,
      likeNum: 134,
      commentNum: 35,
      type: 5
    },
    {
      id: '6',
      title: '本地生活服务行业发展报告发布：市场规模突破500亿元',
      desc: '近日，某权威机构发布了《2023年本地生活服务行业发展报告》，报告显示，我市本地生活服务市场规模已突破500亿元...',
      cover: '',
      addTime: '2023-06-10 13:20:00',
      viewNum: 956,
      likeNum: 62,
      commentNum: 15,
      type: 6
    }
  ],
  
  // 模拟头条详情数据
  detail: {
    id: 1005,
    title: "旅行安全提示：雨季出行注意事项",
    cover: "https://picsum.photos/600/300?travel=17",
    type: "安全提示",
    date: "2025-06-10",
    author: "客服部",
    content: `
        <p>进入雨季，为保障您的旅行安全和顺利，CNT旅行社特别提醒您注意以下事项：</p>
        <p><strong>一、出行前准备</strong></p>
        <ul>
          <li>关注目的地天气预报，随身携带雨具和防水衣物</li>
          <li>检查证件和贵重物品的防水保护措施</li>
          <li>准备常用药品，预防感冒、肠胃不适等问题</li>
        </ul>
        <p><strong>二、行程中注意</strong></p>
        <ul>
          <li>避免前往山区、河谷等易发生山洪、滑坡的危险区域</li>
          <li>乘坐正规交通工具，避免乘坐超载或无运营资质的车辆</li>
          <li>涉水行走时注意观察路况，避开深水区域和漩涡</li>
          <li>如遇恶劣天气，及时寻找安全场所躲避</li>
        </ul>
        <p><strong>三、应急联系方式</strong></p>
        <ul>
          <li>CNT旅行社24小时客服热线：400-888-8888</li>
          <li>目的地紧急救援电话：110（国内）、当地旅游局热线</li>
        </ul>
        <p>旅行途中请保持通讯畅通，随时关注当地天气和安全信息。祝您旅途愉快！</p>
      `,
    tags: ["旅行安全", "雨季", "出行提示"],
    views: 765,
    likes: 52,
    shares: 23,
    comments: 0,
    store: {
      id: 1,
      logo: 'https://picsum.photos/100/100?random=1',
      name: '卡夫卡少儿艺术培训中心',
      distance: '1.2km',
      isCollected: false
    }
  },
  
  // 模拟评论数据
  comments: [
    {
      id: "CM20250601",
      parentId: "",
      userAvatar: "https://picsum.photos/100/100?user=1",
      userName: "李**",
      rating: 5,
      content: "非常满意这次黄山之旅，导游专业负责，行程安排合理，酒店干净舒适，性价比很高，下次还会选择这家旅行社。",
      images: [
        "https://picsum.photos/200/200?travel=10",
        "https://picsum.photos/200/200?travel=11"
      ],
      tags: ["服务周到", "行程合理", "风景优美"],
      date: "2025-05-20",
      replies: []
    },
    {
      id: "CM20250602",
      parentId: "",
      userAvatar: "https://picsum.photos/100/100?user=2",
      userName: "王**",
      rating: 4,
      content: "杭州西湖两日游体验不错，导游讲解很详细，酒店位置很好，离西湖很近，唯一不足的是第一天行程有点赶。",
      images: [],
      tags: ["导游专业", "位置优越", "行程紧凑"],
      date: "2025-05-15",
      replies: []
    }
  ],
  
  // 模拟广告数据
  ads: [
    {
      type: 'multi',
      title: '卡夫卡少儿艺术培训中心特惠课程',
      currentPrice: 199,
      originalPrice: 899,
      discount: '2.2折',
      distance: '2.5km',
      isAd: true,
      images: [
        'https://picsum.photos/300/300?random=2',
        'https://picsum.photos/300/300?random=3',
        'https://picsum.photos/300/300?random=4'
      ]
    },
    {
      type: 'single',
      title: '卡夫卡少儿艺术培训中心特惠课程',
      currentPrice: 199,
      originalPrice: 899,
      discount: '2.2折',
      distance: '2.5km',
      isAd: true,
      images: [
        'https://picsum.photos/300/300?random=2',
        'https://picsum.photos/300/300?random=3',
        'https://picsum.photos/300/300?random=4'
      ]
    }
  ],
  
  // 模拟优惠券数据
  coupons: [
    {
      id: "C20250601",
      name: "满减券",
      type: 1,
      price: 0,
      amount: 10,
      minAmount: 30,
      discountRate: null,
      maxDiscount: null,
      title: "每满80减20元券",
      description: "美食类通用",
      useInstructions: "下单时自动抵扣，不可与其他优惠叠加"
    },
    {
      id: "C20250602",
      name: "折扣券",
      type: 2,
      price: 0,
      amount: null,
      minAmount: 100,
      discountRate: 0.85,
      maxDiscount: 50,
      title: "全场85折券",
      description: "酒店类通用",
      useInstructions: "入住时出示使用，不包含早餐"
    }
  ]
};

// 模拟请求延迟
const mockDelay = (ms = 300) => new Promise(resolve => setTimeout(resolve, ms));

// 模拟响应格式
const mockResponse = (data, success = true, message = 'success') => ({
  code: success ? 0 : 1,
  data,
  message
});

// 开发模式标志
const isDevMode = true; // 设为true使用模拟数据，false使用真实API

export default {
  // 获取头条分类
  getCategories: () => {
    if (isDevMode) {
      return (async () => {
        await mockDelay();
        return mockResponse(mockData.categories);
      })();
    }
    return request.get('/toutiao/categories');
  },
  
  // 获取头条列表
  getList: (params) => {
    if (isDevMode) {
      return (async () => {
        await mockDelay();
        // 根据分类ID过滤数据
        let filteredList = [...mockData.list];
        if (params && params.type) {
          filteredList = mockData.list.filter(item => item.type === params.type);
        }
        return mockResponse(filteredList);
      })();
    }
    return request.get('/toutiao/list', params);
  },
  
  // 获取头条详情
  getDetail: (id) => {
    if (isDevMode) {
      return (async () => {
        await mockDelay();
        // 模拟根据ID返回详情，这里简单返回固定数据
        return mockResponse(mockData.detail);
      })();
    }
    return request.get(`/toutiao/detail/${id}`);
  },
  
  // 获取评论列表
  getComments: (id) => {
    if (isDevMode) {
      return (async () => {
        await mockDelay();
        return mockResponse(mockData.comments);
      })();
    }
    return request.get(`/toutiao/${id}/comments`);
  },
  
  // 获取广告信息
  getAds: () => {
    if (isDevMode) {
      return (async () => {
        await mockDelay();
        return mockResponse(mockData.ads);
      })();
    }
    return request.get('/toutiao/ads');
  },
  
  // 获取优惠券列表
  getCoupons: () => {
    if (isDevMode) {
      return (async () => {
        await mockDelay();
        return mockResponse(mockData.coupons);
      })();
    }
    return request.get('/toutiao/coupons');
  },
  
  // 点赞
  like: (id) => {
    if (isDevMode) {
      return (async () => {
        await mockDelay(200);
        return mockResponse({ success: true });
      })();
    }
    return request.post(`/toutiao/${id}/like`);
  },
  
  // 评论
  comment: (id, data) => {
    if (isDevMode) {
      return (async () => {
        await mockDelay(500);
        return mockResponse({ success: true });
      })();
    }
    return request.post(`/toutiao/${id}/comment`, data);
  },
  
  // 分享
  share: (id) => {
    if (isDevMode) {
      return (async () => {
        await mockDelay(200);
        return mockResponse({ success: true });
      })();
    }
    return request.post(`/toutiao/${id}/share`);
  }
};